package org.rsbot.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JPanel;
import org.rsbot.bot.Bot;
import org.rsbot.bot.RSLoader;
import org.rsbot.client.input.Canvas;
import org.rsbot.event.EventManager;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.Mouse;

public class BotPanel extends JPanel
{
  private static final long serialVersionUID = 2269767882075468055L;
  private Bot bot;
  private BotHome home;
  private HomeUpdater updater;
  private int offX;
  private boolean present;

  public BotPanel(BotHome paramBotHome)
  {
    this.home = paramBotHome;
    this.updater = new HomeUpdater(null);
    setSize(new Dimension(765, 503));
    setMinimumSize(new Dimension(765, 503));
    setPreferredSize(new Dimension(765, 503));
    setBackground(Color.black);
    paramBotHome.setSize(getWidth(), getHeight());
    addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent paramComponentEvent) {
        BotPanel.this.home.setSize(BotPanel.this.getWidth(), BotPanel.this.getHeight());
        if (BotPanel.this.bot != null) {
          BotPanel.this.bot.resize(BotPanel.this.getWidth(), BotPanel.this.getHeight());
          BotPanel.this.offset();
        }
        BotPanel.this.requestFocus();
      }
    });
    addMouseListener(new MouseListener()
    {
      public void mouseClicked(MouseEvent paramMouseEvent) {
        BotPanel.this.redispatch(paramMouseEvent);
        if (!BotPanel.this.hasFocus())
          BotPanel.this.requestFocus();
      }

      public void mouseEntered(MouseEvent paramMouseEvent)
      {
      }

      public void mouseExited(MouseEvent paramMouseEvent)
      {
        BotPanel.this.redispatch(paramMouseEvent);
      }

      public void mousePressed(MouseEvent paramMouseEvent) {
        BotPanel.this.redispatch(paramMouseEvent);
      }

      public void mouseReleased(MouseEvent paramMouseEvent) {
        BotPanel.this.redispatch(paramMouseEvent);
      }
    });
    addMouseMotionListener(new MouseMotionListener()
    {
      public void mouseDragged(MouseEvent paramMouseEvent) {
        BotPanel.this.redispatch(paramMouseEvent);
      }

      public void mouseMoved(MouseEvent paramMouseEvent) {
        BotPanel.this.redispatch(paramMouseEvent);
      }
    });
    addMouseWheelListener(new MouseWheelListener()
    {
      public void mouseWheelMoved(MouseWheelEvent paramMouseWheelEvent) {
        BotPanel.this.redispatch(paramMouseWheelEvent);
      }
    });
    addKeyListener(new KeyListener()
    {
      public void keyPressed(KeyEvent paramKeyEvent) {
        BotPanel.this.redispatch(paramKeyEvent);
      }

      public void keyReleased(KeyEvent paramKeyEvent) {
        BotPanel.this.redispatch(paramKeyEvent);
      }

      public void keyTyped(KeyEvent paramKeyEvent) {
        BotPanel.this.redispatch(paramKeyEvent);
      }
    });
  }

  public void offset() {
    if (this.bot.getCanvas() != null)
    {
      this.offX = ((getWidth() - this.bot.getCanvas().getWidth()) / 2);
    }
  }

  public void setBot(Bot paramBot) {
    if (this.bot != null)
      this.bot.setPanel(null);
    else {
      this.updater.stop();
    }
    this.bot = paramBot;
    if (paramBot != null) {
      paramBot.setPanel(this);
      if (paramBot.getCanvas() != null)
        offset();
    }
    else {
      new Thread(this.updater).start();
    }
  }

  public void paintComponent(Graphics paramGraphics) {
    super.paintComponent(paramGraphics);
    if (this.bot == null)
      this.home.paint(paramGraphics);
    else
      paramGraphics.drawImage(this.bot.getImage(), this.offX, 0, null);
  }

  private void redispatch(MouseEvent paramMouseEvent)
  {
    if ((this.bot != null) && (this.bot.getLoader().getComponentCount() > 0)) {
      Mouse localMouse = this.bot.getMethodContext().mouse;
      if (localMouse == null) {
        return;
      }
      boolean bool = localMouse.isPresent();
      Component localComponent = this.bot.getLoader().getComponent(0);

      paramMouseEvent.translatePoint(-this.offX, 0);

      dispatchHuman(localComponent, paramMouseEvent);
      if ((!this.bot.overrideInput) && ((this.bot.inputFlags & 0x1) == 0)) {
        return;
      }
      if ((paramMouseEvent.getX() > 0) && (paramMouseEvent.getX() < localComponent.getWidth()) && (paramMouseEvent.getY() < localComponent.getHeight()) && (paramMouseEvent.getID() != 505))
      {
        if (bool) {
          if ((paramMouseEvent instanceof MouseWheelEvent)) {
            MouseWheelEvent localMouseWheelEvent = (MouseWheelEvent)paramMouseEvent;
            localComponent.dispatchEvent(new MouseWheelEvent(localComponent, paramMouseEvent.getID(), System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, paramMouseEvent.isPopupTrigger(), localMouseWheelEvent.getScrollType(), localMouseWheelEvent.getScrollAmount(), localMouseWheelEvent.getWheelRotation()));
          }
          else
          {
            localComponent.dispatchEvent(new MouseEvent(localComponent, paramMouseEvent.getID(), System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, paramMouseEvent.isPopupTrigger(), paramMouseEvent.getButton()));
          }
        }
        else {
          localComponent.dispatchEvent(new MouseEvent(localComponent, 504, System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, false));
        }
      }
      else if (bool)
        localComponent.dispatchEvent(new MouseEvent(localComponent, 505, System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, false));
    }
  }

  private void redispatch(KeyEvent paramKeyEvent)
  {
    if (this.bot != null) {
      EventManager localEventManager = this.bot.getEventManager();
      if (localEventManager != null) {
        localEventManager.dispatchEvent(paramKeyEvent);
      }
      if (((this.bot.overrideInput) || ((this.bot.inputFlags & 0x2) != 0)) && (this.bot.getLoader().getComponentCount() > 0)) {
        Component localComponent = this.bot.getLoader().getComponent(0);
        localComponent.dispatchEvent(paramKeyEvent);
      }
    }
  }

  private void dispatchHuman(Component paramComponent, MouseEvent paramMouseEvent) {
    if ((paramMouseEvent.getX() > 0) && (paramMouseEvent.getX() < paramComponent.getWidth()) && (paramMouseEvent.getY() < paramComponent.getHeight()) && (paramMouseEvent.getID() != 505))
    {
      if (this.present) {
        this.bot.getEventManager().dispatchEvent(paramMouseEvent);
      } else {
        this.present = true;
        this.bot.getEventManager().dispatchEvent(new MouseEvent(paramComponent, 504, System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, false));
      }

    }
    else if (this.present) {
      this.present = false;
      this.bot.getEventManager().dispatchEvent(new MouseEvent(paramComponent, 505, System.currentTimeMillis(), 0, paramMouseEvent.getX(), paramMouseEvent.getY(), 0, false));
    }
  }

  private class HomeUpdater
    implements Runnable
  {
    private boolean running;

    private HomeUpdater()
    {
    }

    public void run()
    {
      synchronized (this) {
        if (this.running) {
          throw new IllegalStateException("Already running!");
        }
        this.running = true;
      }
      while (true) {
        synchronized (this) {
          if (!this.running) {
            break label82;
          }
        }
        BotPanel.this.repaint();
        try {
          Thread.sleep(70L);
        }
        catch (java.lang.Exception ) {
        }
      }
      synchronized (this) {
        label82: this.running = false;
      }
    }

    public void stop() {
      synchronized (this) {
        this.running = false;
      }
    }
  }
}